<template>
	<view class="page">
		<view class="zy-address-item" v-for="(item,index) in addressList" :key="index" >
			<uni-list :border="false" itemStyle="border-radius:35rpx">
				<uni-list-item  itemStyle="border-radius:35rpx" @click="checkAddress(item)" :clickable="true">
					<view slot="left" class="slot-box">
						{{item.realName}}
					</view>
					<text class="slot-text" style="margin-left: 10rpx;">{{item.phone}}</text>
				</uni-list-item>
				<view class="zy-describe" @click="checkAddress(item)">
					{{item.school}}{{item.storiedBuilding}}{{item.area}}
				</view>
				<uni-list-item  itemStyle="border-radius:35rpx">
					<view slot="left"  class="slot-box" @click.stop="isChecked(item)">
						<zywork-icon v-if="item.isDefault==1"   type="icongou" color="#00aa7f" size="40"  />
						<view v-else  class="zy-checked"></view>
						<text class="slot-text" style="margin-left: 10rpx;">设为默认地址</text>
					</view>
					<view slot="right"  class="slot-box">
						<zywork-icon   type="iconbianji"  size="32"  color="#888888" @click.stop="addAddress('edit', item)"/>
						<view  class="slot-text" style="margin-left: 10rpx;" @click.stop="addAddress('edit', item)">编辑</view>
						<zywork-icon   type="iconicon_huabanfuben" color="#888888" style="margin-left: 20rpx;"  size="40"  @click.stop="deleteAddress(item)"/>
						<view  class="slot-text" style="margin-left: 10rpx;" @click.stop="deleteAddress(item)">删除</view>
					</view>
				</uni-list-item>
			</uni-list>
		</view>
		<view class="placeholder">
			
		</view>
		<view class="zy-add" @click="addAddress('add')">
			新增地址
		</view>
	</view>
</template>

<script>
	import uniList from '@/components/uni-list/uni-list.vue'
	import uniListItem from '@/components/uni-list-item/uni-list-item.vue'
	import zyworkIcon from '@/components/zywork-icon/zywork-icon.vue'
	import {
		doPostJson,
		doGet,
		nullToStr,
		showInfoToast,
		showSuccessToast
	} from '@/common/util.js'
	import * as ResponseStatus from '@/common/response-status.js'
	export default {
		components: {
			uniList,
			uniListItem,
			zyworkIcon
		},
		data() {
			return {
				loadMoreText: "加载中...",
				showLoadMore: false,
				source: 0, // 来源，如果是1，则表示是从下单页面过来的
				addressList: [],
				pager: {
					pageNo: 1,
					pageSize: 10,
					isActive: 0
				},
			};
		},
		onLoad(option) {
			this.source = option.source;
			this.loadData('init');
		},
		onShow() {
			this.loadData('init');
		},
		onPullDownRefresh() {
			this.pager.pageNo = 1;
			this.loadData('pullDown');
		},
		onReachBottom() {
			this.pager.pageNo += 1;
			this.loadData('reachBottom');
		},
		methods:{
			/**
			 * 加载数据
			 */
			loadData(type) {
				this.loadAddressList(type)
			},
			/**
			 * 加载地址列表数据
			 */
			loadAddressList(type) {
				if(type=="init"){
					this.pager.pageNo = 1;
				}
				uni.showLoading({
					title: '加载中'
				})
				doPostJson('/shipping-address/user/pager-cond-list', this.pager, {}, true).then(response => {
					let [error, res] = response;
					if (res.data.code === ResponseStatus.OK) {
						const rows = nullToStr(res.data.data.rows);
						if (type === 'init') {
							this.addressList = rows;
						} else if (type === 'pullDown') {
							this.addressList = rows;
							uni.stopPullDownRefresh();
							this.showLoadMore = false;
							this.loadMoreText = '加载中...';
						} else if (type === 'reachBottom') {
							if (rows.length > 0) {
								this.addressList = this.addressList.concat(rows);
								this.loadMoreText = '加载更多';
							} else {
								this.loadMoreText = '已加载全部';
							}
						}
					} else {
						showInfoToast(res.data.message);
					}
					uni.hideLoading()
				}).catch(err => {
					console.log(err);
				})
			},
			// 新增地址
			addAddress(type, item) {
				let isDefault = 0
				if ((type === 'add' && this.addressList.length === 0) || (type === 'edit' && this.addressList.length === 1)) {
					isDefault = 1
				}
				uni.navigateTo({
					url: `/pages/address/add_address?type=${type}&data=${JSON.stringify(item)}&isDefault=${isDefault}`
				})
			},
			/**
			 * 删除收货地址
			 */
			deleteAddress(item) {
				uni.showModal({
					content: '您确定要删除收货地址吗？',
					success: (res) => {
						if (res.confirm) {
							doGet("/shipping-address/user/remove/" + item.id, {}, {}, true).then(response => {
								let [error, res] = response;
								if (res.data.code === ResponseStatus.OK) {
									this.refreshList();
								} else {
									showInfoToast(res.data.message);
								}
							}).catch(err => {
								console.log(err);
							})
						}
					}
				})
			},
			isChecked(item){
				let data = {
					id:item.id,
					isDefault:item.isDefault==1?0:1
				}
				doPostJson("/shipping-address/user/is-default", data, {}, true).then(response => {
					let [error, res] = response;
					if (res.data.code === ResponseStatus.OK) {
						this.refreshList()
					} else {
						showInfoToast(res.data.message)
					}
				}).catch(err => {
					console.log(err)
				})
			},
			//选择地址
			checkAddress(item) {
				console.log(this.source)
				if (this.source == 1) {
					//this.$api.prePage()获取上一页实例，在App.vue定义
					this.$api.prePage().selectedAddress = item;
					this.$api.prePage().getSchoolParcelList()
					this.$api.prePage().getSchoolParceList()
					uni.navigateBack()
				}
			},
			//添加或修改成功之后回调
			refreshList() {
				this.loadAddressList('init')
			}
		}
	}
</script>

<style lang="scss">
	@import '@/common/zywork-main.scss';
	page{
		width: 100%;
		background-color: #eeeeee;
		height: 100%;
		padding-top: 30rpx;
	}
	.placeholder{
		width: 100%;
		height: 120rpx;
	}
	.zy-address-item{
		margin: 0rpx $padding-left-right;
		margin-bottom: 30rpx;
		width: 690rpx;
		height: 250rpx;
		background-color: $primary-backcolor;
		border-radius: 35rpx;
	}
	
	.slot-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		
	}
	
	.zy-describe{
		width: 90%;
		margin: 0rpx $padding-left-right;
		padding-bottom: 20rpx;
		font-size: $font-base;
		color: $button-background-disabed-color;
		border-bottom: solid 1rpx $border-color;
	}
	
	.zy-checked{
		width: 40rpx;
		height: 40rpx;
		border: solid 1rpx #00aa7f;
		border-radius: 50%;
	}
	
	.zy-add{
		position: fixed;
		z-index: 999;
		left:150rpx;
		bottom: 20rpx;
		width: 450rpx;
		height: 80rpx;
		line-height: 80rpx;
		background-color: #00aa7f;
		color: $primary-color;
		border-radius: 35rpx;
		text-align: center;
	}
</style>
